package com.next

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
@Transactional(readOnly = true)
class SubscribeController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def springSecurityService

    def index(Integer max)
    {
        def username = springSecurityService?.getPrincipal()?.username
        def subscribeList = Subscribe.findAll("from Subscribe where user.username = '${username}' order by createDate desc", [max: params.max ?: 10, offset: params.offset ?: 0])
        def count = Subscribe.executeQuery("select count(id) from Subscribe where user.username = '${username}'")

        respond subscribeList, model: [subscribeCount: count]
    }

    def show(Subscribe subscribe)
    {
        respond subscribe
    }

    def create()
    {
        respond new Subscribe(params)
    }

    @Transactional
    def save(Subscribe subscribe)
    {
        if (subscribe == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (subscribe.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond subscribe.errors, view: 'create'
            return
        }

        subscribe.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'subscribe.label', default: 'Subscribe'), subscribe.id])
                redirect subscribe
            }
            '*' { respond subscribe, [status: CREATED] }
        }
    }

    def edit(Subscribe subscribe)
    {
        respond subscribe
    }

    @Transactional
    def update(Subscribe subscribe)
    {
        if (subscribe == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (subscribe.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond subscribe.errors, view: 'edit'
            return
        }

        subscribe.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'subscribe.label', default: 'Subscribe'), subscribe.id])
                redirect subscribe
            }
            '*' { respond subscribe, [status: OK] }
        }
    }

    @Transactional
    def delete(Subscribe subscribe)
    {

        if (subscribe == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        subscribe.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'subscribe.label', default: 'Subscribe'), subscribe.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'subscribe.label', default: 'Subscribe'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    def searchSubscribe()
    {
        def username = springSecurityService?.getPrincipal()?.username
        def subject = params["subject"]

        String sql = "from Subscribe as s where s.user.username = '${username}'"
        if (subject)
        {
            sql += " and s.subject like '%${subject}%'"
        }
        String countSql = "select count(s.id) " + sql
        sql += ' order by createDate desc'

        def list = Subscribe.findAll(sql, [max: params.max ?: 10, offset: params.offset ?: 0])
        def count = Subscribe.executeQuery(countSql)

        def subscribe = new Subscribe(params)
        respond list, model: [subscribeCount: count[0], subscribe: subscribe, params: params], view: 'index'
    }
}
